package com.h819.examples.web.db.dbcpexamples;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.lang.StringUtils;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;

import static java.lang.System.out;


/**   
 * @Title: DbcpTestServlet.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2010-3-17 
 * @version V1.0   
 */
public class DbcpTestServlet extends HttpServlet {

	
	private DataSource dataSource = null;
	private QueryRunner runner = null;
	private Connection conn = null;
	
	QueryRunnerExample example = new QueryRunnerExample();
	
	/**
	 * Constructor of the object.
	 */
	public DbcpTestServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		initDataSourceWeb();
		runner = new QueryRunner(dataSource);
		
		String sql = "INSERT INTO user (USERNAME, PASSWORDHASH) values (?, ?)";
		try {
			out.println("\n"
					+ StringUtils.center("Test QueryRunner batch", 80, '*'));
			int[] result = runner.batch(sql, new Object[][] {
					{ "user1", "pwd1" }, { "user2", "pwd2" },
					{ "user3", "pwd3" }, { "user4", "pwd4" } });
			out.printf("运行结果：%s\n", Arrays.toString(result));
		} catch (SQLException ex) {
			DbUtils.printStackTrace(ex);
		}
		
		//Example.printDataSourceStats(dataSource);
	}

	
	private Connection initDataSourceWeb() {

		if (conn == null) {
			if (dataSource == null) {
				InitialContext ctx;
				try {
					ctx = new InitialContext();
					dataSource = (DataSource) ctx
							.lookup("java:/comp/env/jdbc/mysql");
				} catch (NamingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}		
			try {
				return dataSource.getConnection();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return conn;
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
